package com.haixiaoke.saas.enterprise.order.controller;

import com.haixiaoke.saas.common.annotation.RepeatSubmit;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.order.domain.TimeRange;
import com.haixiaoke.saas.order.service.ITimeRangeService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 价格区间Controller
 *
 * @author xiaolong
 * @date 2023-10-24
 */
@RestController
@RequestMapping("/enterprise/order/range")
public class EnterpriseTimeRangeController extends BaseController {
    @Autowired
    private ITimeRangeService timeRangeService;

    /**
     * 查询价格区间列表
     */
    @PreAuthorize("@ss.hasPermi('business:range:list')")
    @GetMapping("/list")
    public AjaxResult list(TimeRange timeRange) {
        List<TimeRange> list = timeRangeService.selectTimeRangeList(timeRange);
        return AjaxResult.success(list);
    }

    /**
     * 导出价格区间列表
     */
    @PreAuthorize("@ss.hasPermi('business:range:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, TimeRange timeRange) {
        List<TimeRange> list = timeRangeService.selectTimeRangeList(timeRange);
        ExcelUtil<TimeRange> util = new ExcelUtil<TimeRange>(TimeRange.class);
        util.exportExcel(response, list, "价格区间数据");
    }

    /**
     * 获取价格区间详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:range:query')")
    @GetMapping(value = "/{timeRangeId}")
    public AjaxResult getInfo(@PathVariable("timeRangeId") Long timeRangeId) {
        return success(timeRangeService.selectTimeRangeByTimeRangeId(timeRangeId));
    }

    /**
     * 新增价格区间
     */
    @PreAuthorize("@ss.hasPermi('business:range:add')")
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody TimeRange timeRange) {
        return toAjax(timeRangeService.insertTimeRange(timeRange));
    }

    /**
     * 修改价格区间
     */
    @PreAuthorize("@ss.hasPermi('business:range:edit')")
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@RequestBody TimeRange timeRange) {
        return toAjax(timeRangeService.updateTimeRange(timeRange));
    }

    /**
     * 删除价格区间
     */
    @PreAuthorize("@ss.hasPermi('business:range:remove')")
    @DeleteMapping("/{timeRangeIds}")
    @RepeatSubmit
    public AjaxResult remove(@PathVariable Long[] timeRangeIds) {
        return toAjax(timeRangeService.deleteTimeRangeByTimeRangeIds(timeRangeIds));
    }
}
